package baiqi.sishen.community.controller;

import baiqi.sishen.community.dto.CommentCreateDTO;
import baiqi.sishen.community.dto.ResultDTO;
import baiqi.sishen.community.dto.ThumbCreateDTO;
import baiqi.sishen.community.execption.CustomizeErrorCode;
import baiqi.sishen.community.execption.CustomizeException;
import baiqi.sishen.community.model.Thumb;
import baiqi.sishen.community.model.User;
import baiqi.sishen.community.service.ThumbService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author 兕神
 * @version 1.0
 * DateTime: 2023/5/7
 */
@Controller
public class ThumbController {

    @Resource
    private ThumbService thumbService;

    @ResponseBody
    @PostMapping("/thumb")
    public Object like(
            @RequestBody ThumbCreateDTO thumbCreateDTO,//@RequestBody接受json格式的数据
            HttpServletRequest request) {
        //获取用户
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            throw new CustomizeException(CustomizeErrorCode.USER_NOT_FOUND);
        }

        //thumbCreateDTO 判断
        if (thumbCreateDTO == null) {
            throw new CustomizeException(CustomizeErrorCode.THUMB_FAIL);
        }
        Thumb thumb = new Thumb();
        thumb.setTargetId(thumbCreateDTO.getTargetId());
        thumb.setLiker(user.getId());
        thumb.setType(thumbCreateDTO.getType());
        thumb.setStatus(thumbCreateDTO.getStatus());

        int code = thumbService.insertOrUpdate(thumb,user);
        if (code == 2012) {
            return ResultDTO.error(CustomizeErrorCode.THUMB_NOT_AGAIN);
        }
        return ResultDTO.success();
    }
}
